package com.glodon.paas.activity.client.demo.document;

import static com.glodon.paas.activity.client.demo.document.DocumentActivityContants.*;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.glodon.paas.activity.api.bean.Activity;
import com.glodon.paas.activity.api.bean.ActivityIn;
import com.glodon.paas.activity.client.ActivityClient;
import com.glodon.paas.activity.client.ActivityClientHttpImpl;
import com.glodon.paas.exception.PaasServerExceptionFactory;
/**
 * 示例类，具体类可由客户端自己实现
 * 
 * @author a
 */
class DocumentActivityClient {

    @Autowired
    ActivityClient activityClient;

    public void setActivityClient(ActivityClient activityClient) {
        this.activityClient = activityClient;
    }

    // TODO 业务方自定义的接收活动的userId集合
    private Collection<String> getReceivers(String userId) {
        return null;
    }

    /**
     * 查询动态
     * 
     */
    public Activity[] query(Map<String, String> params) {
        return activityClient.query(params);
    }

    // 上传文件
    public void uploadFile(String actorId, String actorName, String itemId, String itemName) {
        ActivityIn activity = new ActivityIn();
        activity.setActorId(actorId);
        activity.setActorName(actorName);
        activity.setItemId(itemId);
        activity.setItemType(DocumentActivityContants.ITEM_TYPE_FILE);
        activity.setItemName(itemName);
        activity.setActivityType(activityType_upload);
        
        activity.addReceiver(getReceivers(actorId));
        activityClient.publish(activity);
    }

    // 通用活动
    public void commonActivity(
                       String actorId, 
                       String actorName, 
                       String itemId, 
                       String ItemType, 
                       String itemName,
                       Long activityType
                       ) {
        ActivityIn activity = new ActivityIn();
        activity.setActorId(actorId);
        activity.setActorName(actorName);
        activity.setItemId(itemId);
        activity.setItemType("" + ItemType);
        activity.setItemName(itemName);
        activity.setActivityType(activityType);

        
        activity.addReceiver(getReceivers(actorId));
        activityClient.publish(activity);
    }

    // 查看文件
    public void viewFile(String actorId, String actorName, String itemId, String itemName) {
        commonActivity(actorId, actorName, itemId, DocumentActivityContants.ITEM_TYPE_FILE, itemName, activityType_view);
    }

    // 下载文件
    public void downFile(String actorId, String actorName, String itemId, String itemName) {
        ActivityIn activity = new ActivityIn();
        activity.setActorId(actorId);
        activity.setActorName(actorName);
        activity.setItemId(itemId);
        activity.setItemName(itemName);
        activity.setItemType(DocumentActivityContants.ITEM_TYPE_FILE);
        activity.setActivityType(activityType_download);

        
        activity.addReceiver(getReceivers(actorId));
        activityClient.publish(activity);
    }

    /**
     * 批量下载文件
     * 
     * @param actorId
     * @param actorName
     * @param itemId
     * @param itemName
     */
    public void batchDownFile(String actorId, String actorName, List<String> itemId, List<String> itemName) {
        ActivityIn activity = new ActivityIn();
        activity.setActorId(actorId);
        activity.setActorName(actorName);
        // activity.setItemId(itemId);
        // activity.setItemName(itemName);
        activity.setItemType(DocumentActivityContants.ITEM_TYPE_FILE);
        activity.setActivityType(activityType_pkgDownload);
        
        int size = itemId.size();
        if (size != itemName.size()) {
            throw PaasServerExceptionFactory.validateException("itemId not match itemName", null);
        }
        // 使用扩展字段提交其他信息
        Map<String,Object> ext = new HashMap<String,Object>();
        ext.put("count", size);        
        ext.put("itemIds", itemId);
        ext.put("itemNames", itemName);

        activity.addReceiver(getReceivers(actorId));
        activityClient.publish(activity);
    }

    // 删除文件
    public void deleteFile(String actorId, String actorName, String itemId, String itemName) {
        ActivityIn activity = new ActivityIn();
        activity.setActorId(actorId);
        activity.setActorName(actorName);
        activity.setItemId(itemId);
        activity.setItemType(DocumentActivityContants.ITEM_TYPE_FILE);
        activity.setItemName(itemName);
        activity.setActivityType(activityType_delete);

        
        activity.addReceiver(getReceivers(actorId));
        activityClient.publish(activity);
    }

    public void publishFile(String actorId, String actorName, String itemId, String itemName, String token) {
        ActivityIn activity = new ActivityIn();
        activity.setActorId(actorId);
        activity.setActorName(actorName);
        activity.setItemId(itemId);
        activity.setItemType(DocumentActivityContants.ITEM_TYPE_FILE);
        activity.setItemName(itemName);
        activity.setActivityType(activityType_publish);

        activity.setExt1(token);
        // activity.putExtension("token", token);

        
        activity.addReceiver(getReceivers(actorId));
        activityClient.publish(activity);

    }

    // 共享文件
    public void shareFile(String actorId, String actorName, String itemId, String itemName) {
        ActivityIn activity = new ActivityIn();
        activity.setActorId(actorId);
        activity.setActorName(actorName);
        activity.setItemId(itemId);
        activity.setItemType(DocumentActivityContants.ITEM_TYPE_FILE);
        activity.setItemName(itemName);
        activity.setActivityType(activityType_share);

        
        activity.addReceiver(getReceivers(actorId));
        activityClient.publish(activity);
    }

    // 取消共享文件
    public void unShareFile(String actorId, String actorName, String itemId, String itemName) {
        ActivityIn activity = new ActivityIn();
        activity.setActorId(actorId);
        activity.setActorName(actorName);
        activity.setItemId(itemId);
        activity.setItemType(DocumentActivityContants.ITEM_TYPE_FILE);
        activity.setItemName(itemName);
        activity.setActivityType(activityType_unshare);

        
        activity.addReceiver(getReceivers(actorId));
        activityClient.publish(activity);
    }

    //client test
    public static void main(String[] args) {
        // 初始化client
        ActivityClientHttpImpl activityClient = new ActivityClientHttpImpl();
        activityClient.setAppId(APPID_DOCUMENT);
        
        DocumentActivityClient client = new DocumentActivityClient();
        client.setActivityClient(activityClient);
        
        // 封装数据
        List<String> itemId = new ArrayList<String>();
        itemId.add("fileId1");
        itemId.add("fileId2");
        
        List<String> itemName = new ArrayList<String>();
        itemName.add("你是人间四月天.doc");
        itemName.add("三国演义.pdf");
        
        // 发送数据
        client.batchDownFile("hc", "黄诚", itemId, itemName);
    }

    public Integer getAppId() {
        return activityClient.getAppId();
    }
}
